package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huiztech.maystall.ui.HorizontalListView;

public class ClassifyActivity extends Activity {

    private HorizontalListView clothListView, shoeListView, boxListView, peishiListView, jiajuListView, yidongListView;
    private List<Map<String, Object>> clothList, shoeList, boxList, peishiList, jiajuList, yidongList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify);

        clothListView = (HorizontalListView) findViewById(R.id.cloth_list);
        shoeListView = (HorizontalListView) findViewById(R.id.shoe_list);
        boxListView = (HorizontalListView) findViewById(R.id.box_list);
        peishiListView = (HorizontalListView) findViewById(R.id.peishi_list);
        jiajuListView = (HorizontalListView) findViewById(R.id.jiaju_list);
        yidongListView = (HorizontalListView) findViewById(R.id.yidong_list);

        clothList = new ArrayList<Map<String, Object>>();
        shoeList = new ArrayList<Map<String, Object>>();
        boxList = new ArrayList<Map<String, Object>>();
        peishiList = new ArrayList<Map<String, Object>>();
        jiajuList = new ArrayList<Map<String, Object>>();
        yidongList = new ArrayList<Map<String, Object>>();

        getData();

        clothListView.setAdapter(new GalleryAdapter(this, clothList));
        shoeListView.setAdapter(new GalleryAdapter(this, shoeList));
        boxListView.setAdapter(new GalleryAdapter(this, boxList));
        peishiListView.setAdapter(new GalleryAdapter(this, peishiList));
        jiajuListView.setAdapter(new GalleryAdapter(this, jiajuList));
        yidongListView.setAdapter(new GalleryAdapter(this, yidongList));

        clothListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Clothing");
            }
        });
        shoeListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Shoe");
            }
        });
        boxListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Handbag");
            }
        });
        peishiListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Accessory");
            }
        });
        jiajuListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Furniture");
            }
        });
        yidongListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                intentProductList("Mobile devices");
            }
        });
    }

    private void intentProductList(String title) {
        Intent intent = new Intent(ClassifyActivity.this, ProductListActivity.class);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    // 测试数据
    private void getData() {
        int[] clothId = new int[] { R.drawable.demo_classify_cloth1, R.drawable.demo_classify_cloth2,
                R.drawable.demo_classify_cloth3, R.drawable.demo_classify_cloth4, R.drawable.demo_classify_cloth5,
                R.drawable.demo_classify_cloth6 };
        int[] jiajuId = new int[] { R.drawable.demo_classify_jiaju1, R.drawable.demo_classify_jiaju2,
                R.drawable.demo_classify_jiaju3, R.drawable.demo_classify_jiaju4, R.drawable.demo_classify_jiaju5,
                R.drawable.demo_classify_jiaju6 };
        int[] peishiId = new int[] { R.drawable.demo_classify_peishi1, R.drawable.demo_classify_peishi2,
                R.drawable.demo_classify_peishi3, R.drawable.demo_classify_peishi4, R.drawable.demo_classify_peishi5,
                R.drawable.demo_classify_peishi6 };
        int[] mobileId = new int[] { R.drawable.demo_classify_mobile1, R.drawable.demo_classify_mobile2,
                R.drawable.demo_classify_mobile3, R.drawable.demo_classify_mobile4, R.drawable.demo_classify_mobile1,
                R.drawable.demo_classify_mobile2 };
        int[] shoeId = new int[] { R.drawable.demo_classify_shoe1, R.drawable.demo_classify_shoe2,
                R.drawable.demo_classify_shoe3, R.drawable.demo_classify_shoe4, R.drawable.demo_classify_shoe5,
                R.drawable.demo_classify_shoe6 };
        int[] handbagId = new int[] { R.drawable.demo_classif_handbag1, R.drawable.demo_classif_handbag2,
                R.drawable.demo_classif_handbag3, R.drawable.demo_classif_handbag4, R.drawable.demo_classif_handbag1,
                R.drawable.demo_classif_handbag2 };

        String[] cloth = new String[] { "长袖衫", "开衫", "小西装", "裙子", "羽绒服", "连帽" };
        for (int i = 0; i < cloth.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", clothId[i]);
            map.put("classify", cloth[i]);
            clothList.add(map);
        }
        String[] mobile = new String[] { "手机", "电脑", "平板", "相机", "耳机", "麦克风" };
        for (int i = 0; i < mobile.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", mobileId[i]);
            map.put("classify", mobile[i]);
            yidongList.add(map);
        }
        String[] jiaju = new String[] { "桌子", "柜子", "沙发", "衣架", "书架", "椅子" };
        for (int i = 0; i < jiajuId.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", jiajuId[i]);
            map.put("classify", jiaju[i]);
            jiajuList.add(map);
        }
        String[] peishi = new String[] { "耳环", "项链", "手镯", "围巾", "发卡", "眼镜" };
        for (int i = 0; i < peishiId.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", peishiId[i]);
            map.put("classify", peishi[i]);
            peishiList.add(map);
        }
        String[] handbag = new String[] { "迷你包", "旅行箱", "双肩包", "手提包", "钱包", "斜挎包" };
        for (int i = 0; i < handbagId.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", handbagId[i]);
            map.put("classify", handbag[i]);
            boxList.add(map);
        }
        String[] shoe = new String[] { "高跟鞋", "平底鞋", "运动鞋", "帆布鞋", "马丁靴", "弹力靴" };
        for (int i = 0; i < shoeId.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", shoeId[i]);
            map.put("classify", shoe[i]);
            Log.i("xxx", shoe[i]);
            shoeList.add(map);
        }

    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

    public class GalleryAdapter extends BaseAdapter {
        private Context mContext;
        private List<Map<String, Object>> mList;
        private LayoutInflater mInflater;

        public GalleryAdapter(Context context, List<Map<String, Object>> list) {
            mContext = context;
            mList = list;
            this.mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return mList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.classify_gallery_item, null);
                holder.imageView = (ImageView) convertView.findViewById(R.id.gallery_item_image);

                holder.textView = (TextView) convertView.findViewById(R.id.gallery_item_text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.imageView.setBackgroundResource(Integer.valueOf(mList.get(position).get("image").toString()));

            holder.textView.setText(mList.get(position).get("classify").toString());

            return convertView;
        }
    }
}
